package com.browsesoft.htmlcomponent;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class HTMLDocPageComponent extends
		com.browsesoft.htmlcomponent.HTMLBasicComponent {
	/**
	 * 元素列表
	 */
	NodeList eList = null;

	String id = null;

	/**
	 * 构造器
	 * 
	 * @param parent
	 *            组件的父组件
	 */

	public HTMLDocPageComponent(HTMLBasicComponent parent) throws Exception {
		super(parent);
	}

	/**
	 * 初始化时创建子组件并设置初始化参数
	 */
	public void init(HttpServletRequest request, Element e) throws Exception {
		super.init(request, e);
		((HTMLDocPageModel) model).setComponent(this);
		this.eList = this.getPage().getDocument().getElementsByTagName(
				"contextarea");
	}

	/**
	 * 根据Model的内容更新需要处理的元素内容，在产生页面时要调用
	 * 如果需要Model处理的元素，则在该方法中调用HTMLBasicModel.updateElement()
	 */
	protected void updateElement() throws Exception {
		super.updateElement();
		for (int i = 0; i < eList.getLength(); i++) {
			Element e = (Element) eList.item(i);
			((HTMLDocPageModel) model).setContext(e, this.getPage()
					.getDocument());
		}
		Element elem = (Element) this.element.getElementsByTagName("input")
				.item(0);
		String onclick = elem.getAttribute("onclick");
		onclick = onclick.replaceAll("point=", "point=" + id);
		onclick = onclick.replaceAll("templatename=", "templatename=附件只读模板");
		onclick = onclick.replaceAll("readonly=", "readonly=true");
		elem.setAttribute("onclick", onclick);
	}

	/**
	 * 处理用户请求，将需要Model处理的内容提交给Model
	 * 
	 * @param request
	 *            客户端请求
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.service(request, response);
		id = request.getParameter("id");
	}
}